﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Platform.ObjectModel
{

	internal static class DataObjectNameExtensions
	{

		/// <summary>
		/// Transforma <paramref name="name"/> em um namespace do dotNet
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public static string ToDotNetNamespace( this DataObjectName name )
		{
			return name.ToDotNetNamespace( null, null );
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		/// <param name="sufix"></param>
		/// <returns></returns>
		public static string ToDotNetNamespace( this DataObjectName name, string sufix )
		{
			return name.ToDotNetNamespace( String.Empty, sufix );
		}

		/// <summary>
		/// Transforma <paramref name="name"/> em um namespace do dotNet
		/// </summary>
		/// <param name="name"></param>
		/// <param name="prefix"></param>
		/// <param name="sufix"></param>
		/// <returns></returns>
		public static string ToDotNetNamespace( this DataObjectName name, string prefix, string sufix )
		{
			if ( name == null )
				return String.Empty;
			else
			{
				StringBuilder ns = new StringBuilder( name.FullName );

				// Altera '[' por '.' e remove ']'
				for ( int index = ns.Length - 1; index >= 0; index-- )
				{
					if ( ns[ index ] == '[' )
						ns[ index ] = '.';
					else if ( ns[ index ] == ']' )
						ns.Remove( index, 1 );
				}

				// Adiciona o prefixo
				if ( !String.IsNullOrEmpty( prefix ) )
					ns.Insert( 0, prefix );

				// Adiciona o sufixo
				if ( !String.IsNullOrEmpty( sufix ) )
					ns.Append( sufix );

				return ns.ToString();
			}
		}

	}
}
